package com.qqtech.qquan.pub.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.mall.dao.MallProductDao;
import com.qqtech.qquan.order.dao.OrderBaseDao;
import com.qqtech.qquan.order.dao.OrderItemDao;
import com.qqtech.qquan.order.enums.OrderBaseCommentStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseOrderTypeDictEnum;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.model.OrderItem;
import com.qqtech.qquan.order.model.OrderItemVo;
import com.qqtech.qquan.pub.dao.PubFeedbackDao;
import com.qqtech.qquan.pub.enums.PubFeedbackObjDictEnum;
import com.qqtech.qquan.pub.model.PubFeedback;
import com.qqtech.qquan.pub.model.PubFeedbackQuery;

@Service
public class PubFeedbackServiceImpl extends BaseServiceImpl<PubFeedback>
		implements PubFeedbackService {
	public static final Logger log = LoggerFactory.getLogger(PubFeedbackServiceImpl.class);
	
	@Resource
	private PubFeedbackDao pubFeedbackDao;
	@Resource 
	private OrderBaseDao orderBaseDao;
	@Resource 
	private OrderItemDao orderItemDao;
	@Resource
	private MallProductDao mallProductDao;

	@Override
	protected BaseDao<PubFeedback> getBaseDao() {
		return pubFeedbackDao;
	}

	@Override
	public int handleSaveFeedback(PubFeedback pubFeedback) {
		int id = pubFeedbackDao.insert(pubFeedback);
		OrderBase ob = orderBaseDao.getByOrderNo(pubFeedback.getOrderNo());
		if (OrderBaseOrderTypeDictEnum.HOMEMAKING.getCode() == ob.getOrderType()) {
			if (ob.getCommentStatus() != OrderBaseCommentStatusDictEnum.ALL.getCode()) {
				OrderBase updateOrder = new OrderBase();
				updateOrder.setId(ob.getId());
				updateOrder.setCommentStatus(OrderBaseCommentStatusDictEnum.ALL.getCode());
				orderBaseDao.updateById(updateOrder);
			}
		} else {
			//更新订单明细 是否评价(TODO 可合并成一个SQL)
			OrderItem orderItemQeury = new OrderItem();
			orderItemQeury.setOrderNo(pubFeedback.getOrderNo());
			orderItemQeury.setProductId(pubFeedback.getProductId());
			OrderItemVo itemVo = orderItemDao.getOne(orderItemQeury);
			if(itemVo != null){
				OrderItem updateItem = new OrderItem();
				updateItem.setId(itemVo.getId());
				updateItem.setIsComment(YesNoEnum.YES.getCode());
				orderItemDao.updateById(updateItem);
			}
			
			//更新订单 评价状态
			OrderItem isCommentItemQeury = new OrderItem();
			isCommentItemQeury.setOrderNo(pubFeedback.getOrderNo());
			isCommentItemQeury.setIsComment(YesNoEnum.YES.getCode());
			//已评论的订单明细数量
			int isCommentCount = orderItemDao.queryCount(isCommentItemQeury);
			//订单明细总数
			OrderItem item = new OrderItem();
			item.setOrderNo(pubFeedback.getOrderNo());
			int orderItemCount = orderItemDao.queryCount(item);
			if(ob.getCommentStatus() != OrderBaseCommentStatusDictEnum.ALL.getCode()){
				OrderBase updateOrder = new OrderBase();
				updateOrder.setId(ob.getId());
				if(isCommentCount > 0){
					if(orderItemCount == isCommentCount){
						updateOrder.setCommentStatus(OrderBaseCommentStatusDictEnum.ALL.getCode());
					}else{
						updateOrder.setCommentStatus(OrderBaseCommentStatusDictEnum.PART.getCode());
					}
				}else{
					updateOrder.setCommentStatus(OrderBaseCommentStatusDictEnum.NONE.getCode());
				}
				//根据订单编号   更新状态
				orderBaseDao.updateById(updateOrder);
			}
		}
		return id;
	}

	@Override
	public Double getScoreAvg(Integer type, Integer productId) {
		Double scoreAvg = 0.0;
		if (type != null && productId != null) {
			PubFeedbackQuery q = new PubFeedbackQuery();
			q.setType(type);
			q.setProductId(productId);
			scoreAvg = pubFeedbackDao.queryScoreAvg(q);
		}
		return scoreAvg;
	}

	@Override
	public int feedbackCount(Integer productId, String hasImg,
			Integer level, Integer score, Integer type, Integer status) {
		return pubFeedbackDao.feedbackCount(productId, hasImg, level, score, type, status);
	}

	@Override
	public int handleSaveFeedbackList(List<PubFeedback> pubFeedbackList) {

		int id = pubFeedbackDao.insertInBatch(pubFeedbackList);
		
		OrderBase ob = orderBaseDao.getByOrderNo(pubFeedbackList.get(0).getOrderNo());
		if (OrderBaseOrderTypeDictEnum.HOMEMAKING.getCode() == ob.getOrderType()) {
			if (ob.getCommentStatus() != OrderBaseCommentStatusDictEnum.ALL.getCode()) {
				OrderBase updateOrder = new OrderBase();
				updateOrder.setId(ob.getId());
				updateOrder.setCommentStatus(OrderBaseCommentStatusDictEnum.ALL.getCode());
				orderBaseDao.updateById(updateOrder);
				return id;
			}
		} else {
			//更新商品评价数量
			for(PubFeedback feedback : pubFeedbackList){
				if (feedback.getObj() != null
						&& feedback.getObj().intValue() == PubFeedbackObjDictEnum.PRODUCT
								.getCode() && feedback.getProductId() != null
						&& feedback.getProductId() != 0) {
					mallProductDao.updateFeedbackCount(feedback.getProductId());
				}
			}
			
			//更新订单明细 是否评价(TODO 可合并成一个SQL)
			OrderItem orderItemQeury = new OrderItem();
			orderItemQeury.setOrderNo(pubFeedbackList.get(0).getOrderNo());
			//orderItemQeury.setProductId(pubFeedbackList.get(0).getProductId());
			List<OrderItem> itemList = orderItemDao.queryList(orderItemQeury);
			
			if(itemList != null && itemList.size() > 0){
				for(OrderItem updateItem:itemList){
					updateItem.setIsComment(YesNoEnum.YES.getCode());
				}
				orderItemDao.updateInBatch(itemList);
			}
			
			//根据订单编号   更新状态
			ob.setCommentStatus(OrderBaseCommentStatusDictEnum.ALL.getCode());
			ob.setUpdAt(TimeUtil.now());
			orderBaseDao.updateById(ob);
			return id;
		
	    }
		return 0;
	}
}